//
//  DepartmentViewController.m
//  掌上海印
//
//  Created by admin on 16/4/13.
//  Copyright © 2016年 handsome. All rights reserved.
//
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#import "DepartmentViewController.h"
#import "AddressBookTableviewCell.h"
#import "Groups.h"
#import "Member.h"
#import "HeaderView.h"
#import "PersonalData.h"
#import "LrdOutputView.h"

@interface DepartmentViewController ()<UITableViewDataSource,UITableViewDelegate,HeaderViewDelegate,LrdOutputViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *listArray;
@property (nonatomic, strong) UIBarButtonItem *btn;
@property (nonatomic, strong) NSArray *dataArr;
@property (nonatomic, strong) LrdOutputView *outputView;
@end

@implementation DepartmentViewController

- (NSArray *)listArray{
    if (!_listArray) {
        NSString * path = [[NSBundle mainBundle] pathForResource:@"list.plist" ofType:nil];
        NSArray * dicArr = [NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray * arr = [NSMutableArray array];
        for (NSDictionary *dict in dicArr) {
            Groups *group = [Groups parsingJsonWithDictionary:dict];
            [arr addObject:group];
        }
        _listArray = arr;
    }
    return _listArray;
}

- (void)viewDidLoad {
    
    self.navigationItem.leftBarButtonItem = nil;
    [super viewDidLoad];
    [self tableView];
    _btn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(CalendarClick :)];
    self.navigationItem.rightBarButtonItem = _btn;
    LrdCellModel *one = [[LrdCellModel alloc] initWithTitle:@"公司电话" imageName:@"cphone"];
    LrdCellModel *two = [[LrdCellModel alloc] initWithTitle:@"公司邮箱" imageName:@"e-mail"];
    LrdCellModel *three = [[LrdCellModel alloc] initWithTitle:@"公司传真" imageName:@"portaiture"];
    self.dataArr = @[one, two, three];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}

//导航栏右按钮点击事件

- (void)CalendarClick :(UIBarButtonItem *)btn
{
    CGFloat x = [[UIScreen mainScreen]bounds].size.width-10;
    CGFloat y = 75;
    _outputView = [[LrdOutputView alloc] initWithDataArray:self.dataArr origin:CGPointMake(x, y) width:125 height:44 direction:kLrdOutputViewDirectionRight];
    _outputView.delegate = self;
    _outputView.dismissOperation = ^(){
    //设置成nil，以防内存泄露
    _outputView = nil;
    };
    [_outputView pop];

}

//右菜单栏点击事件
- (void)didSelectedAtIndexPath:(NSIndexPath *)indexPath {
    

}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.rowHeight = 60;
        _tableView.sectionHeaderHeight = 44;
        _tableView.backgroundColor = bgColor;
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    Groups *group = self.listArray[section];
    return group.isOpen? group.groups.count:0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AddressBookTableviewCell *cell = [AddressBookTableviewCell AddressBookTableViewCellWithTableView:tableView];
    if (self.listArray) {
        Groups *group = self.listArray[indexPath.section];
        Member *member = group.groups[indexPath.row];
        cell.member = member;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PersonalData *personal = [[PersonalData alloc] init];
    Groups *group = self.listArray[indexPath.section];
    Member *member = group.groups[indexPath.row];
    personal.member = member;
    personal.group = group;
    NSLog(@"zhejiushiai--------%@",member.name);
    [self.navigationController pushViewController:personal animated:YES]; //跳转到下一页面
    
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    HeaderView *header = [HeaderView HeadViewWithTableView:tableView];
    header.delegate = self;
    if (self.listArray) {
        Groups *group = self.listArray[section];
        header.group = group;
    }
    return header;
}

- (void)HeaderView:(HeaderView *)view didButton:(UIButton *)sender
{
    [self.tableView reloadData];
    
}


@end
